<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*课程管理*/
class GymCourse extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	function index(){
		echo $this->fetch();
	}	
	function add(){
		$this->assign("type",input("type",1));
		echo $this->fetch();
	}
	/*新增员工*/
	function add_post(){
		if(request()->isPost()){
			db('GymCourse')->insert(array(
									'shop_id'=>sp_get_current_shop_id(),
									'name'=>input("name"),
									'price'=>input('price'),
									"descs"=>input('descs'),
									"type"=>input('type'),
									));
			$this->ajax_success("成功!");	
		}else{
			$this->ajax_error("非法操作!");
		}
	}
	/*修改员工信息*/
	function edit(){
		$id=input("id",0);
		$this->assign("model",db("GymCourse")->where("id=$id")->find());
		echo $this->fetch();			
	}
	function edit_post(){
		$id=input("id");
		db('GymCourse')->where("id=$id")->update(array(
						"name"=>input("name"),
						"price"=>input("price"),
						"descs"=>input("descs"),
						"type"=>input("type"),
						));
		$this->ajax_success("成功！");	
	}
	
	function delete(){
		if(request()->isPost()){
			$id=input("id");
			db("GymCourse")->where("id=$id")->delete();
			$this->ajax_success("成功");
		}else{
			$this->ajax_error("非法操作！");
		}
	}
	
	/*员工列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10071000");
		$this->assign("menus",$auth_group);
	}
	
	/*获取员工数据*/
	private function _ajax_index(){
		$shop_id=sp_get_current_shop_id();
		$query=array(
					 'name'=>array("field" =>"name","operator" =>"like"),
					);
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		if(!empty($shop_id)){
			$where_and.=" and shop_id=$shop_id";
		}
		$total=db("GymCourse")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("GymCourse")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
